//
//  WBImageController.m
//  WBImageView
//
//  Created by Tec_one on 15/12/16.
//  Copyright (c) 2015年 徐锐. All rights reserved.
//

#import "WBImageController.h"
#import "UIImageView+WebCache.h"
#import "BJDynamicGoodArticleCell.h"

#define kItemWidth self.view.bounds.size.width
#define kItemHeight self.view.bounds.size.height

@interface WBImageController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
}

@end

@implementation WBImageController

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    /*
     // 将像素point由point所在视图转换到目标视图view中，返回在目标视图view中的像素值
     - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
     // 将像素point从view中转换到当前视图中，返回在当前视图中的像素值
     - (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
     
     // 将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
     - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
     // 将rect从view中转换到当前视图中，返回在当前视图中的rect
     - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
     */
    
    self.view.backgroundColor = [UIColor blackColor];
    //将相对于灰色view坐标系的 图片的矩形区域 换算到 控制器的view的坐标上
    CGRect newFrame = [self.currentItem.superview convertRect:self.currentItem.frame toView:self.view];
    
    self.currentItem.frame = newFrame;
    
    [self.view addSubview:self.currentItem];
    
    [UIView animateWithDuration:.3 animations:^{
        
        
        self.currentItem.frame = newFrame;
        
    } completion:^(BOOL finished) {
        
        self.currentItem.frame = self.currentItem.originFrame;
        
        //使用完item的动画后 将item放回原父视图
        [self.wbImageView addSubview:self.currentItem];
        
        //当视图要出现时,滚动到对应index的图片处
        [_scrollView scrollRectToVisible:CGRectMake(kItemWidth * _index, 0, kItemWidth, kItemHeight) animated:NO];
        
        _scrollView.hidden  = NO;
        
        [self.view bringSubviewToFront:_scrollView];
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)setDataList:(NSArray *)dataList{

    _dataList = dataList;
    
    /*_______________________________________________________________*/
    
    _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    
    _scrollView.contentSize = CGSizeMake(kItemWidth * _dataList.count, kItemHeight);
    
    _scrollView.pagingEnabled = YES;
    
    _scrollView.delegate = self;
    
    _scrollView.hidden = YES;
    
    [self.view addSubview:_scrollView];
    
    /*_______________________________________________________________*/


    for (int i = 0; i<_dataList.count; i++) {
        
        WBImageViewItem *imageV = [[WBImageViewItem alloc]initWithFrame:CGRectMake(kItemWidth * i, 0, kItemWidth, kItemHeight) withTarget:self action:@selector(dismiss)];
                
        NSString *smallUrl = _dataList[i];
        
//        NSRange range = [smallUrl rangeOfString:@"thumbnail"];
//
//        NSString *largeUrl = [smallUrl stringByReplacingCharactersInRange:range withString:@"large"];
        
        
        [imageV sd_setImageWithURL:[NSURL URLWithString:smallUrl]];
        
        imageV.contentMode = UIViewContentModeScaleAspectFit;
        
        [_scrollView addSubview:imageV];
        
    }

}

- (void)dismiss{
    
    [self weiboViewBringToFront];
    
    //通过当前显示的图片的下标 获取灰色视图上对应的item
    WBImageViewItem *item = [self.wbImageView.items objectAtIndex:_index];
    
    //将做动画的图片 拿到子视图的最上端 防止被其他图片遮挡
    [self.wbImageView bringSubviewToFront:item];
    
    //将WBImageController上的(0,0)点 转换到 灰色view的坐标系上 作为放大的图片的左上角坐标
    CGPoint originPoint = [self.view convertPoint:CGPointMake(0, 0) toView:self.wbImageView];
    
    item.frame = CGRectMake(originPoint.x, originPoint.y, kItemWidth, kItemHeight);
    
    [self dismissViewControllerAnimated:NO completion:^{
        
        [UIView animateWithDuration:.3 animations:^{
           
            item.frame = item.originFrame;
            
        }];
        
    }];

}

- (void)weiboViewBringToFront{
    
    UIResponder *next = self.wbImageView.nextResponder;
    
    while (next) {
                
        if ([next isKindOfClass:[BJDynamicGoodArticleCell class]]) {

            BJDynamicGoodArticleCell *cell = (BJDynamicGoodArticleCell *)next;

            [cell.contentView bringSubviewToFront:cell.imgsBGView];

            return;
        }

        next = next.nextResponder;
        
    }
    
    
}


#pragma mark - UIScrollViewDelegate

//滑动时更新当前的index
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    _index = scrollView.contentOffset.x/kItemWidth;

}

@end
